local start, duration = GetSpellCooldown("Shadow Word: Pain");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then return 0 end
  return cd
end

local SWPminDuration = 4;  
local VTminDuration = 4;  
local nextspellcd = 0.2;

local cdMB = GetCooldown("Mind Blast");
local cdt6 = GetCooldown("Divine Star");
local cdPoM = GetCooldown("Prayer of Mending");

local _, _, startswp, swpduration = IconAura("HARMFUL", "target", "Shadow Word: Pain", "player");
local _, _, startvt, vtduration = IconAura("HARMFUL", "target", "Vampiric Touch", "player");


if (not (UnitExists("target") and UnitCanAttack("player", "target"))) or (UnitIsDeadOrGhost("target")) then
  return
end

if (UnitPower("Player", 13) == 3) then
  return IconSpell("Devouring Plague", true);
end

if (cdMB <= nextspellcd) then  
  return IconSpell("Mind Blast", true);
end

if (not swpduration) or (startswp + swpduration - GetTime() < SWPminDuration) then
  return IconSpell("Shadow Word: Pain", true);
end

if (not vtduration) or (startvt + vtduration - GetTime() < VTminDuration) then
  return IconSpell("Vampiric Touch", true);
end

local c = floor(UnitHealth("target") / UnitHealthMax("target") * 100)
if (c <= 20 and GetCooldown("Shadow Word: Death")<= 1) then
    return IconSpell("Shadow Word: Death", true)
end

if UnitBuff("player", "Surge of Darkness") then 
    return IconSpell("Mind Spike", true);
end

if (cdt6 <= nextspellcd) then  
  return IconSpell("Divine Star", true);
end

if (cdPoM <= nextspellcd) then  
  return IconSpell("Prayer of Mending", true);
end

return IconSpell("Mind Flay", true);